package com.ujs.ucenter.controller;

import com.ujs.commonutils.constant.R;
import com.ujs.servicebase.entity.MemberVoOrder;
import com.ujs.ucenter.entity.Member;
import com.ujs.ucenter.entity.vo.RegisterVo;
import com.ujs.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangshihao
 * @create 2023-08-11 16:17
 */
@RestController
@RequestMapping("/educenter/member")
@Api(tags = "用户管理api")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @PostMapping("/register")
    @ApiOperation("用户注册方法")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @PostMapping("/login")
    @ApiOperation("登录方法")
    public R login(@RequestBody Member member) {
        // 返回token值
        String token = memberService.login(member);
        System.out.println(token);
        return R.ok().data("token", token);
    }

    @GetMapping("/getMemberInfo")
    @ApiOperation("根据token获取用户信息")
    public R getMemberInfo(HttpServletRequest request) {
        Member member = memberService.getMemberInfo(request);
        return R.ok().data("member", member);
    }

    @GetMapping("/getMemberInfo/{memberId}")
    @ApiOperation("根据用户id获取用户信息")
    public MemberVoOrder getMemberInfoById(@PathVariable("memberId") String memberId) {
        Member member = memberService.getMemberInfoById(memberId);
        MemberVoOrder memberVoOrder = new MemberVoOrder();
        BeanUtils.copyProperties(member, memberVoOrder);
        return memberVoOrder;
    }

    @ApiOperation("查询某天的注册人数")
    @GetMapping("/countRegister/{day}")
    public Integer countRegister(@PathVariable("day") String day) {
        return memberService.countRegister(day);
    }
}
